package com.sx.pipeline.wxapi;

/**
 * author : zhouyouqiang
 * e-mail : 1638473898@qq.com
 * date   : 2022-07-22 9:54
 * desc   :
 * version: 1.0
 */
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.sx.mvvmlibs.bus.RxBus;
import com.sx.pipeline.R;
import com.sx.pipeline.event.PayResultEvent;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.jetbrains.annotations.Nullable;
public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {

    public static Object targetPage;

    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, getString(R.string.wx_app_id));
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            PayResultEvent event;
            switch (resp.errCode) {
                case 0:
                    event = new PayResultEvent(true, "支付成功", targetPage);
                    Log.d("okgowxpaylog", "支付成功");
                    break;
                default:
                    event = new PayResultEvent(false, "支付失败", targetPage);
                    Log.d("okgowxpaylog", "支付失败");
                    Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show();
                    break;
            }
            RxBus.getDefault().post(event);
            targetPage = null;
            finish();
        }
    }

}
